package com.example.smartcitydemo.viewModel.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.bean.*
import com.example.smartcitydemo.ext.getLiveData
import com.example.smartcitydemo.repo.home.HomePageRepo

class HomeViewModel : ViewModel() {
    private val newsTypeMutable: MutableLiveData<String> = MutableLiveData()

    private val homeBannerMutable: MutableLiveData<String> = MutableLiveData()

    private val mutableServiceData: MutableLiveData<String> = MutableLiveData()

    private val mutableNewsTypeId: MutableLiveData<Int> = MutableLiveData()

    private val mutableNewsDetailId: MutableLiveData<Int> = MutableLiveData()

    private val mutableNewsDoLikeID: MutableLiveData<Int> = MutableLiveData()

    private val mutableLogin: MutableLiveData<LoginBean> = MutableLiveData()

    private val mutableNewsReply: MutableLiveData<Int> = MutableLiveData()

    private val mutableCommitLike: MutableLiveData<Int> = MutableLiveData()

    private val mutableSubmitCommit: MutableLiveData<NewsCommitBean> = MutableLiveData()

    private val mutableNewsSearch: MutableLiveData<String> = MutableLiveData()


    val newsTypeLiveData: LiveData<Resource<List<NewsTypBean>>> = newsTypeMutable.switchMap {
        getLiveData { HomePageRepo.getNewsType() }
    }


    val homeBannerBean: LiveData<Resource<List<HomeBannerBean>>> = homeBannerMutable.switchMap {
        getLiveData { HomePageRepo.getHomeBanner() }
    }

    val ServiceTypeData: LiveData<Resource<List<ServiceBean>>> =
        mutableServiceData.switchMap {
            getLiveData { HomePageRepo.getServiceData() }
        }

    val newsListData: LiveData<Resource<List<NewsInfo>>> =
        mutableNewsTypeId.switchMap {
            getLiveData { HomePageRepo.getNewsInfo(it) }
        }

    val newsDetailData: LiveData<Resource<NewsInfo>> =
        mutableNewsDetailId.switchMap {
            getLiveData { HomePageRepo.getNewsDetail(it) }
        }

    val doLikeResult: LiveData<Resource<String>> =
        mutableNewsDoLikeID.switchMap {
            getLiveData { HomePageRepo.doLikeArticle(it) }
        }

    val doLoginResult: LiveData<Resource<String>> =
        mutableLogin.switchMap {
            getLiveData { HomePageRepo.doLogin(it) }
        }

    val newsReplyInfo: LiveData<Resource<List<ReplyInfo>>> =
        mutableNewsReply.switchMap {
            getLiveData { HomePageRepo.getNewsReply(it) }
        }

    val likeCommitResult: LiveData<Resource<String>> =
        mutableCommitLike.switchMap {
            getLiveData { HomePageRepo.doLikeCommit(it) }
        }

    val submitResult: LiveData<Resource<String>> =
        mutableSubmitCommit.switchMap {
            getLiveData { HomePageRepo.submitCommit(it) }
        }

    val newsSearchResult = mutableNewsSearch.switchMap {
        getLiveData { HomePageRepo.getNewsBySearch(it) }
    }

    fun getNewsType() {
        newsTypeMutable.value = newsTypeMutable.value
    }


    fun getHomeBanner() {
        homeBannerMutable.value = homeBannerMutable.value
    }


    fun getServiceTypeData() {
        mutableServiceData.value = mutableServiceData.value
    }

    fun getNewsInfo(typeID: Int) {
        mutableNewsTypeId.value = typeID
    }

    fun getNewsDetail(newsDetailId: Int) {
        mutableNewsDetailId.value = newsDetailId
    }

    fun doLikeArticle(articleId: Int) {
        mutableNewsDoLikeID.value = articleId
    }

    fun doLogin(loginBean: LoginBean) {
        mutableLogin.value = loginBean
    }

    fun getNewsReply(newsId: Int) {
        mutableNewsReply.value = newsId
    }

    fun doLikeCommit(commitId: Int) {
        mutableCommitLike.value = commitId
    }

    fun submitCommit(newsCommitBean: NewsCommitBean) {
        mutableSubmitCommit.value = newsCommitBean
    }

    fun getNewsSearch(title: String) {
        mutableNewsSearch.value = title
    }
}